package com.example.demo.controller;



import com.example.demo.pojo.*;
import com.example.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.logging.Logger;

/**
 * @author yanglup
 * @create 2020-11-18 22:08
 */
@RestController
public class MytestController {
    @Autowired
    GoodsService goodsService;
    Logger log= Logger.getLogger("获取值");


    /**获取openid*/
    @RequestMapping("/login")
    public String login(@RequestParam String code, @RequestParam String from){
        log.info("loginpage获取到的code以及appid数据--> code=" + code+"appid="+from);
        String url="https://api.weixin.qq.com/sns/jscode2session?appid="+
                from+"&secret=7ba28e46f7c097ae41f5b3881c57e817&js_code="+
                code+"&grant_type=authorization_code";

        RestTemplate rt=new RestTemplate();
        String resp=rt.getForObject(url,String.class);
        log.info("返回值="+resp);
        return resp;
    }

//图片上传 多张图片插入第一个    时间戳
    /**插入商品数据 */
    @RequestMapping("/goods/goodsadd")
    public void goodsadd(Goodsadd goodsadd){
//        System.out.println(goodsadd);

        goodsService.GoodsaddServiceInsert(goodsadd);
    }

    /**商品分类*/
    @RequestMapping("/categories")
    public Map<String, Object> categories(){
        List<Goodsfenlei2> list2=goodsService.Goodsqueryfenlei2(0,49);
        List<Goodsfenlei2> list3=goodsService.Goodsqueryfenlei2(49,3);
        List<Goodsfenlei2> list4=goodsService.Goodsqueryfenlei2(52,3);
        List<Goodsfenlei2> list5=goodsService.Goodsqueryfenlei2(55,4);
        List<Goodsfenlei2> list6=goodsService.Goodsqueryfenlei2(59,6);
        List<Goodsfenlei2> list7=goodsService.Goodsqueryfenlei2(65,3);
        List<Goodsfenlei2> list8=goodsService.Goodsqueryfenlei2(68,16);
        List<Goodsfenlei2> list9=goodsService.Goodsqueryfenlei2(84,4);
        List<Goodsfenlei2> list10=goodsService.Goodsqueryfenlei2(88,9);
        List<Goodsfenlei2> list11=goodsService.Goodsqueryfenlei2(97,8);
        List<Goodsfenlei2> list12=goodsService.Goodsqueryfenlei2(105,16);
        List<Goodsfenlei2> list13=goodsService.Goodsqueryfenlei2(121,7);


        List<Goodsfenlei1> list=goodsService.Goodsqueryfenlei1();

        Goodsfenlei1 goodsfenlei1 = new Goodsfenlei1(list.get(0).getCat_id(), list.get(0).getCat_name(), list2);
        Goodsfenlei1 goodsfenlei2 = new Goodsfenlei1(list.get(1).getCat_id(), list.get(1).getCat_name(), list3);
        Goodsfenlei1 goodsfenlei3 = new Goodsfenlei1(list.get(2).getCat_id(), list.get(2).getCat_name(), list4);
        Goodsfenlei1 goodsfenlei4 = new Goodsfenlei1(list.get(3).getCat_id(), list.get(3).getCat_name(), list5);
        Goodsfenlei1 goodsfenlei5 = new Goodsfenlei1(list.get(4).getCat_id(), list.get(4).getCat_name(), list6);
        Goodsfenlei1 goodsfenlei6 = new Goodsfenlei1(list.get(5).getCat_id(), list.get(5).getCat_name(), list7);
        Goodsfenlei1 goodsfenlei7 = new Goodsfenlei1(list.get(6).getCat_id(), list.get(6).getCat_name(), list8);
        Goodsfenlei1 goodsfenlei8 = new Goodsfenlei1(list.get(7).getCat_id(), list.get(7).getCat_name(), list9);
        Goodsfenlei1 goodsfenlei9 = new Goodsfenlei1(list.get(8).getCat_id(), list.get(8).getCat_name(), list10);
        Goodsfenlei1 goodsfenlei10 = new Goodsfenlei1(list.get(9).getCat_id(), list.get(9).getCat_name(), list11);
        Goodsfenlei1 goodsfenlei11 = new Goodsfenlei1(list.get(10).getCat_id(), list.get(10).getCat_name(), list12);
        Goodsfenlei1 goodsfenlei12 = new Goodsfenlei1(list.get(11).getCat_id(), list.get(11).getCat_name(), list13);


        List<Goodsfenlei1> mylist=new ArrayList<>();
        mylist.add(goodsfenlei1);
        mylist.add(goodsfenlei2);
        mylist.add(goodsfenlei3);
        mylist.add(goodsfenlei4);
        mylist.add(goodsfenlei5);
        mylist.add(goodsfenlei6);
        mylist.add(goodsfenlei7);
        mylist.add(goodsfenlei8);
        mylist.add(goodsfenlei9 );
        mylist.add(goodsfenlei10);
        mylist.add(goodsfenlei11);
        mylist.add(goodsfenlei12);
        Map<String,Object> mymap=new HashMap<>();
        mymap.put("msg","获取成功");
        mymap.put("status",200);
        Map<String,Object> map=new HashMap<>();
        map.put("message",mylist);
        map.put("meta",mymap);
        return map;
    }

    /**商品列表搜索--------有参数传入query查询，cid通过id去查询某类商品，pagenum第一页 pageesize页面大小 */
    @RequestMapping("/goods/search")
    public Map<String,Object> goodssearch(Integer cat_one_id,Integer cat_two_id,Integer pagenum,Integer pagesize){

        List<Goodlist> list=goodsService.Goodsquerylistservice(cat_one_id,cat_two_id,pagenum-1,pagesize);
//        System.out.println(pagenum-1+"--"+pagesize);

        Integer mytotal=goodsService.Goodsquerylistcount(cat_one_id,cat_two_id);

        Map<String,Object> messagemap=new HashMap<>();
        messagemap.put("total",mytotal);//控制页面总条数，可以sql查询
        messagemap.put("pagenum",1);
        messagemap.put("goods",list);
        Map<String,Object> mymap=new HashMap<>();
        mymap.put("msg","获取成功");
        mymap.put("status",200);
        Map<String,Object> map=new HashMap<>();
        map.put("message",messagemap);
        map.put("meta",mymap);
        return map;

    }

    /**商品详情------------有参数传入goods_id*/
    @RequestMapping("/goods/detail")
    public Map<String,Object> detail(Integer goods_id){
        //先插入3张图片   如果没有就插入，如果有就将查到的显示
        goodsService.Goodsdetail2insert(goods_id);
        //再查询3张图片
        List<Goodsdetail2> list2=goodsService.Goodsquerydetail2(goods_id);


        //基本上不改变Goodsdetail1
        Goodsdetail1 mygoodsdetail1=goodsService.Goodsquerydetail(goods_id);
        Goodsdetail1 goodsdetail1=new Goodsdetail1(mygoodsdetail1.getGoods_id(),mygoodsdetail1.getCat_id(),mygoodsdetail1.getGoods_name(),
                                                   mygoodsdetail1.getGoods_price(),mygoodsdetail1.getGoods_number(),mygoodsdetail1.getGoods_described(),
                                                    mygoodsdetail1.getGoods_oldnew(),mygoodsdetail1.getSchool(),mygoodsdetail1.getGoods_small_logo(),
                                                    mygoodsdetail1.getStatus(),list2);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
//        map.put("message", list2);
        map.put("message", goodsdetail1);
        map.put("meta", mymap);
        return map;
    }

    /**搜索图书书名------------有参数传入query*/
    @RequestMapping("/goods/qsearch")
    public Map<String,Object> qsearchbookname(String query){
        List<Goodsquerybookname> list=goodsService.Goodsquerybookname(query);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }

    /**反馈页面*/
    @RequestMapping("/feedback")
    public void feedback(Feedback feedback){
        System.out.println(feedback);
        goodsService.Feedback(feedback);
    }


    /**删除商品*/
    @RequestMapping("/goods/deletegoods")
    public Map<String, Object> deletegoods(String openid, Integer goods_id){
        goodsService.deletegoods(openid,goods_id);

        Map<String, Object> map = new HashMap<>();
        map.put("msg", "获取成功");
        map.put("status", 200);
        return map;
    }

}
